package org.nbict.iot.protocol;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.nbict.iot.protocol.config.ProtocolMapper;
import org.nbict.iot.protocol.context.FieldTypeContext;
import org.nbict.iot.protocol.context.ParserContext;
import org.nbict.iot.protocol.type.ProtocolField;
import org.nbict.iot.protocol.util.BytesOperator;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;

/**
 * Created by songseven on 18/6/16.
 */
public class ProtocolTask implements Runnable {

    private ProtocolField field;
    private byte[] raw;
    private CountDownLatch latch;

    public ProtocolTask(ProtocolField field, CountDownLatch latch) {
        this.field = field;
        this.latch = latch;
        raw = BytesOperator.hexStr2Bytes("232303fe4c4a55373058315a3448473535313739350100b512071309182d01010301000000020bf105fa271645010eea60000002010101544e204e205005fa27100500073f9e5c01c979340601150efb010d0ef20101470101470700000000000000000008010105fa03ee00280001280ef80ef40ef50ef50ef50efa0ef80ef70ef80ef70ef40ef50ef40ef50ef50ef50ef40ef40ef10ef50efb0efa0ef80ef80efa0ef80ef80efa0ef70ef50ef50ef80efa0ef70ef40ef40ef40efa0ef70ef50901010008474747474747474766");
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            ParserContext context = new FieldTypeContext(raw, field); //0x02为实时信息上报

            JSON re = (JSON) context.type().process(context);
            System.out.println(re.toJSONString());
            //System.out.println(Thread.currentThread().getName() + " => success");
        }
        latch.countDown();
    }

    public static void main(String[] args) throws IOException,
            InterruptedException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        ProtocolMapper mapper = session.getMapper(ProtocolMapper.class);
        ProtocolField rootField = mapper.getProtocolFieldById(120);

        byte[] raw = BytesOperator.hexStr2Bytes("232302FE4C4A55373058315A5848473530313738380100A71207130B3009010201010000000116F605DC266C3C0100EA60000005000742740301C839EA0601060EBE01020EB50101470103460700000000000000000008010105DC034400280001280EB60EB50EB80EB90EBB0EBE0EBB0EB90EBB0EB60EB90EB60EB60EB90EB60EB90EB90EB90EBB0EB90EBB0EBE0EB60EBB0EBB0EB80EB80EB80EB90EB60EB50EB60EB80EB90EBC0EB90EB90EB60EB90EB80901010008474746474746474725");

        ParserContext context = new FieldTypeContext(raw, rootField); //0x02为实时信息上报

        JSON re = (JSON) context.type().process(context);

        System.out.println(re.toJSONString());

        //ExecutorService executorService = Executors.newFixedThreadPool(3);;

//        int count = 4;
//        CountDownLatch latch = new CountDownLatch(count);
//
//        Long start = System.currentTimeMillis();
//        for (int i = 0; i < count; i++) {
//            //executorService.submit(new ProtocolTask(rootField));
//            new Thread(new ProtocolTask(rootField, latch)).start();
//        }
////        executorService.awaitTermination(10, TimeUnit.SECONDS);
//        //executorService.shutdown();
//        latch.await();
//        Long end = System.currentTimeMillis();
//        System.out.println("during ==> " + (end - start));
    }
}
